//
//  UIView + Toast.swift
//  Edaixi-iOS
//
//  Created by 刘树华 on 2017/11/29.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation
import UIKit



extension UIView {
    
    func makeToast(msg:String,duration:TimeInterval,position:Any,rotation:CGAffineTransform?=nil) -> UIView {
        return makeToast(msg: msg, title: nil, position: position, duration: duration,rotation:rotation)
    }
    
    func makeToast(msg:String,duration:TimeInterval,rotation:CGAffineTransform?=nil) -> UIView {
        return makeToast(msg: msg, title: nil, position: ToastPositionCenter, duration: duration,rotation:rotation)
    }
    
    func makeToast(msg:String,rotation:CGAffineTransform?=nil) -> UIView {
        
        return makeToast(msg: msg, title: nil, position: ToastPositionCenter, duration: 3,rotation:rotation)
    }
    
    func makeToast(msg:String?=nil,title:String?=nil,position:Any,duration:TimeInterval,rotation:CGAffineTransform?=nil)-> UIView {
        
        return makeToast(msg: msg, title: title, position: position, duration: duration,style:ToastStyleManager.Shared.sharedStyle,rotation:rotation)
        
    }
    func makeToast(msg:String?=nil,title:String?=nil,position:Any,duration:TimeInterval,style:ToastStyle = ToastStyleManager.Shared.sharedStyle,rotation:CGAffineTransform?=nil) -> UIView {
        let toast = ToastView()
//        toast.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
        toast.setUpToast(title: title, message: msg, style:  style,des:self,rotation: rotation)
        showToast(toast: toast, duration: duration, position: position)
        return toast
    }
    
   private func showToast (toast:UIView,duration:TimeInterval,position:Any) {
       toast.center = toastCenterPoint(position: position, toast: toast)
        toast.alpha = 0
//    MARK 处理多个Toast的问题
//   let lastToast = self.subviews.filter{$0.center == toast.center}
//    if lastToast.count > 0{
//      toast.center.y = toast.center.y + (lastToast.last?.bounds.height)!
//    }
    
        self.addSubview(toast)
    
        UIView.animate(withDuration: ToastStyleManager.Shared.sharedStyle.fadeDuration, delay: 0, options: UIViewAnimationOptions.curveEaseOut.union(.allowUserInteraction), animations: {
            toast.alpha = 1
        }) { (finished) in
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+duration, execute: {
                self.hideToast(toast: toast)
            })
        }
    }
    
    func hideAllActivitysTast()  {
        self.subviews.filter{$0 is ToastView}.forEach{hideToast(toast: $0)}
    }
    
     func hideToast(toast:UIView){
        UIView.animate(withDuration: ToastStyleManager.Shared.sharedStyle.fadeDuration, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState.union(.curveEaseIn), animations: {
            toast.alpha = 0
        }, completion: { (finished) in
            toast.removeFromSuperview()
        })
    }
   private func toastCenterPoint (position:Any,toast:UIView) -> CGPoint {
        let style = ToastStyleManager.Shared.sharedStyle
        if let value = position as? String{
            if value == ToastPositionTop {
                
                return CGPoint(x: self.bounds.width/2, y: toast.frame.height/2+(style?.verticalPadding)!)
            }else if value == ToastPositionCenter {
                
                return CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
            }
        }else if let value = position as? NSValue {
            return value.cgPointValue
        }
       
        return CGPoint(x: self.bounds.size.width/2, y:  self.bounds.height-(toast.frame.height/2)-(style?.verticalPadding)!)
    }
}

